package cn.lx.middleware.thread.pool.sdk.trigger.job;

import cn.lx.middleware.thread.pool.sdk.domain.model.entity.ThreadPoolConfigEntity;
import cn.lx.middleware.thread.pool.sdk.domain.service.DynamicThreadPoolService;
import cn.lx.middleware.thread.pool.sdk.registry.RegistryService;
import com.alibaba.fastjson.JSON;
import io.micrometer.core.instrument.ImmutableTag;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author wcy
 * @version 1.0
 * @description: 线程池数据上报任务
 * @date 2025-06-10 下午12:47
 */
public class ThreadPoolDataReportJob {

    private final Logger logger = LoggerFactory.getLogger(ThreadPoolDataReportJob.class);

    private final DynamicThreadPoolService dynamicThreadPoolService;

    private final RegistryService registry;

    public ThreadPoolDataReportJob(DynamicThreadPoolService dynamicThreadPoolService, RegistryService registry) {
        this.dynamicThreadPoolService = dynamicThreadPoolService;
        this.registry = registry;
    }

    @Scheduled(cron = "0/20 * * * * ?")
    public void execReportThreadPoolList() {
        List<ThreadPoolConfigEntity> threadPoolConfigEntities = dynamicThreadPoolService.queryThreadPoolList();
        registry.reportThreadPool(threadPoolConfigEntities);
        logger.info("动态线程池，上报线程池信息：{}", JSON.toJSONString(threadPoolConfigEntities));

        for (ThreadPoolConfigEntity threadPoolConfigEntity : threadPoolConfigEntities) {
            registry.reportThreadPoolConfigParameter(threadPoolConfigEntity);
            logger.info("动态线程池，上报线程池配置：{}", JSON.toJSONString(threadPoolConfigEntity));
        }

    }


}
